Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Java 8 Interview Questions and Answers

Question: How lambda expression and functional interfaces are related?
Answer:
Lambda expressions can only be applied to abstract method of functional interface.
For example:
Runnable has only one abstract method called run, so it can be used as below:
// Using lambda expression
Thread t1=new Thread(
  ()->System.out.println("In Run method")
);

Here we are using Thread constructor which takes Runnable as parameter. As you can see we did not specify any function name here, as Runnable has only one abstract method, java will implicitly create anonymous Runnable and execute run method.
It will be as good as below code.
Thread t1=new Thread(new Runnable() { 
   @Override
   public void run() {
      System.out.println("In Run method");
   }
});
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook